<template>
  <div :class="className == '移观测桩' ? '' : 'app-container'">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="">
        <el-select filterable clearable style="width: 140px" v-model="queryParams.railwayId" placeholder="线名">
          <el-option v-for="dict in railwayNamearr" :label="dict.railwayName" :value="dict.railwayId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-select filterable clearable style="width: 140px" v-model="queryParams.stationId" placeholder="车站名">
          <el-option v-for="dict in stationNamearr" :label="dict.stationName" :value="dict.stationId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="">
        <el-select filterable clearable style="width: 140px" v-model="queryParams.trackId" placeholder="股道编号">
          <el-option v-for="dict in trackCodearr" :label="dict.trackCode" :value="dict.trackId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="" class="formitemcla">
        <el-input v-model="queryParams.startMileageNumber" placeholder="起点里程" clearable style="width: 140px" type="number">
          <template #suffix> km </template>
        </el-input>
      </el-form-item>
      <el-form-item label="~">
        <el-input v-model="queryParams.endMileageNumber" placeholder="终点里程" clearable style="width: 140px" type="number">
          <template #suffix> km </template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery" style="width: 90px">查询</el-button>
        <el-button @click="resetQuery" style="width: 90px">重置</el-button>
        <el-button type="primary" style="width: 90px" @click="handleExport">导出</el-button>
        <el-button type="primary" @click="handleAdd" style="width: 90px">新增</el-button>
        <el-button @click="childValuefun" style="width: 90px">返回</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="pileList" height="62vh" border>
      <!-- <el-table-column label="单位id"  prop="unitId" />
      <el-table-column label="线路id"  prop="railwayId" /> -->
      <el-table-column label="线路" prop="railwayName" />
      <el-table-column label="车站" prop="stationName" />
      <el-table-column label="股道编号" prop="trackCode" />
      <el-table-column label="起点里程" prop="startMileage" />
      <el-table-column label="终点里程" prop="endMileage" />
      <!-- <el-table-column label="起点里程(数字版)"  prop="startMileageNumber" />
      <el-table-column label="终点里程(数字版)"  prop="endMileageNumber" /> -->
      <el-table-column label="钢轨编号" prop="railCode" />
      <!--  <el-table-column label="铺设日期"  prop="layDate" />
      <el-table-column label="健康值"  prop="healthIndex" />
      <el-table-column label="隐患等级"  prop="hiddenDangerLevel" />
      <el-table-column label="设备使用频率"  prop="equipmentUsageFrequency" />
      <el-table-column label="设备综合效率(OEE)"  prop="oee" />
      <el-table-column label="平均无故障工作时间(MTBF)"  prop="mtbf" />
      <el-table-column label="平均修复时间(MTTR)"  prop="mttr" />
      <el-table-column label="区段"  prop="zone" />
      <el-table-column label="工区"  prop="workArea" />
      <el-table-column label="人员"  prop="user" />
      <el-table-column label="备注"  prop="remark" /> -->
      <el-table-column label="直接数据" min-width="110">
        <template #default="scope" class="buttontext">
          <el-button link type="primary" @click="handleUpdate(scope.row)">参数</el-button>
          <el-button link type="primary" @click="meansFun(scope.row)">资料</el-button>
        </template>
      </el-table-column>
      <el-table-column label="关联数据" min-width="210">
        <template #default="scope" class="buttontext">
          <el-button link type="primary" @click="resumeUpdate(scope.row)">作业履历</el-button>
          <el-button link type="primary" @click="deviceProblem(scope.row)">设备问题</el-button>
          <el-button link type="primary" @click="businessFinance(scope.row)">业财</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <!-- <el-button link type="primary" @click="handleUpdate(scope.row)">修改</el-button> -->
          <el-button link type="primary" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog draggable title="资料" v-model="meansopen" width="1300px" append-to-body @close="closefun">
      <means :forma="formam" parentName="站内移观测桩" v-if="meansopen"></means>
    </el-dialog>
    <!-- 添加或修改正线轨道设备-无缝线路关联设备-位移观测桩信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1250px" append-to-body draggable>
      <el-form ref="form" :model="forma" :rules="rules" label-width="100px" :inline="true">
        <el-form-item label="单位名称" prop="unitId">
          <el-select filterable clearable style="width: 180px" v-model="forma.unitId" placeholder="请选择">
            <el-option v-for="dict in unitNameobj" :label="dict.unitName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="线名" prop="railwayId">
          <el-select filterable clearable style="width: 180px" v-model="forma.railwayId" placeholder="请选择">
            <el-option v-for="dict in railwayNameobj" :label="dict.railwayName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车站名" prop="stationId">
          <el-select filterable clearable style="width: 180px" v-model="forma.stationId" placeholder="请选择">
            <el-option v-for="dict in stationNameobj" :label="dict.stationName" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="股道编号" prop="trackId">
          <el-select filterable clearable style="width: 180px" v-model="forma.trackId" placeholder="请输入">
            <el-option v-for="dict in trackListobj" :label="dict.trackCode" :value="dict.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="起点里程" prop="startMileageNumber">
          <el-input style="width: 180px" type="number" v-model="forma.startMileageNumber" placeholder="请输入里程" />
        </el-form-item>
        <el-form-item label="终点里程" prop="endMileageNumber">
          <el-input style="width: 180px" type="number" v-model="forma.endMileageNumber" placeholder="请输入里程" />
        </el-form-item>
        <el-form-item label="钢轨编号" prop="railCode">
          <el-input style="width: 180px" v-model="forma.railCode" placeholder="请输入钢轨编号" />
        </el-form-item>
        <el-form-item label="铺设日期" prop="layDate">
          <el-date-picker style="width: 180px" v-model="forma.layDate" type="datetime" placeholder="请输入铺设日期" />
        </el-form-item>
        <el-form-item label="健康值" prop="healthIndex">
          <el-input style="width: 180px" v-model="forma.healthIndex" placeholder="请输入健康值" />
        </el-form-item>
        <el-form-item label="隐患等级" prop="hiddenDangerLevel">
          <el-input style="width: 180px" v-model="forma.hiddenDangerLevel" placeholder="请输入隐患等级" />
        </el-form-item>
        <el-form-item label="设备使用频率" prop="equipmentUsageFrequency">
          <el-input style="width: 180px" v-model="forma.equipmentUsageFrequency" placeholder="请输入设备使用频率" />
        </el-form-item>
        <el-form-item label="设备综合效率(OEE)" prop="oee">
          <el-input style="width: 180px" v-model="forma.oee" placeholder="请输入设备综合效率(OEE)" />
        </el-form-item>
        <el-form-item label="平均无故障工作时间(MTBF)" prop="mtbf">
          <el-input style="width: 180px" v-model="forma.mtbf" placeholder="请输入平均无故障工作时间(MTBF)" />
        </el-form-item>
        <el-form-item label="平均修复时间(MTTR)" prop="mttr">
          <el-input style="width: 180px" v-model="forma.mttr" placeholder="请输入平均修复时间(MTTR)" />
        </el-form-item>
        <el-form-item label="区段" prop="zone">
          <el-input style="width: 180px" v-model="forma.zone" placeholder="请输入区段" />
        </el-form-item>
        <el-form-item label="工区" prop="workArea">
          <el-input style="width: 180px" v-model="forma.workArea" placeholder="请输入工区" />
        </el-form-item>
        <el-form-item label="人员" prop="user">
          <el-input style="width: 180px" v-model="forma.user" placeholder="请输入人员" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input style="width: 180px" v-model="forma.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="joint">
const props = defineProps({
  className: {
    type: String,
    default: "",
  },
  childValuefun: {
    type: Function,
    default: () => {},
  },
});
import {
  insertManRailFacilityObservationPileapi,
  selectManRailFacilityObservationPileListapi,
  editManRailFacilityObservationPileapi,
  deleteManRailFacilityObservationPileByIdapi,
  selectManBasicRailwayListapi,
  selectManBasicUnitListapi,
  selectManBasicStationListapi,
  selectManStationFacilityTrackListapi,
} from "@/api/main/api.js";
import { useContent } from "@/mixin/index.js";
const { meansopen, meansFun, formam, closefun } = useContent();
import means from "@/components/railmodle/means.vue";

import { reactive, ref, toRefs } from "vue";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const title = ref("移观测桩信息新增");
const open = ref(false);
const data = reactive({
  // 基础数据-单位数据表格数据
  pileList: [],
  // 查询参数
  queryParams: {
    railwayId: "",
    railwayName: "",
    startMileageNumber: "",
    endMileageNumber: "",
    stationId: "",
    trackId: "",
    trackCode: "",
  },

  railwayNameobj: [],
  unitNameobj: [],
  // 表单参数
  forma: {},
  // 表单校验
  rules: {
    unitId: [{ required: true, message: "单位名称不能为空", trigger: ["blur", "change"] }],
    railwayId: [{ required: true, message: "线路名称不能为空", trigger: ["blur", "change"] }],
    stationId: [{ required: true, message: "车站名称不能为空", trigger: ["blur", "change"] }],
    trackId: [{ required: true, message: "股道编号不能为空", trigger: ["blur", "change"] }],
    startMileageNumber: [{ required: true, message: "起点里程不能为空", trigger: ["blur", "change"] }],
    endMileageNumber: [{ required: true, message: "终点里程不能为空", trigger: ["blur", "change"] }],
    layDate: [{ required: true, message: "铺设日期不能为空", trigger: ["blur", "change"] }],
  },
  editobj: {},
  railwayNamearr: [],
  stationNamearr: [],
  stationNameobj: [],
  trackListobj: [],
  trackCodearr: [],
});
const {
  trackCodearr,
  stationNameobj,
  trackListobj,
  stationNamearr,
  railwayNamearr,
  queryParams,
  forma,
  rules,
  pileList,
  unitNameobj,
  railwayNameobj,
  editobj,
} = toRefs(data);

/** 查询基础数据-移观测桩数据列表 */
function getList() {
  loading.value = true;
  selectManRailFacilityObservationPileListapi(queryParams.value).then((res) => {
    if (res.code == 200) {
      pileList.value = res.data;
      loading.value = false;
      const xian = {};
      railwayNamearr.value = pileList.value.reduce((next, item) => {
        xian[item.railwayName] == undefined ? (xian[item.railwayName] = true & next.push(item)) : "";
        return next;
      }, []);
      const daoc = {};
      stationNamearr.value = pileList.value.reduce((next, item) => {
        daoc[item.stationName] == undefined ? (daoc[item.stationName] = true & next.push(item)) : "";
        return next;
      }, []);
      const gud = {};
      trackCodearr.value = pileList.value.reduce((next, item) => {
        gud[item.trackCode] == undefined ? (gud[item.trackCode] = true & next.push(item)) : "";
        return next;
      }, []);
    }
  });
}
getList();
//重置
function resetQuery() {
  queryParams.value = {
    railwayId: "",
    railwayName: "",
    startMileageNumber: "",
    endMileageNumber: "",
    stationId: "",
    trackId: "",
    trackCode: "",
  };
  getList();
}
/** 查询基础数据-单位数据列表 */
function getList1() {
  selectManBasicRailwayListapi({ hasDelete: 0 }).then((res) => {
    if (res.code == 200) {
      railwayNameobj.value = res.data;
    }
  });
}
getList1();
/** 查询基础数据-线名数据列表 */
function getList2() {
  selectManBasicUnitListapi({ hasDelete: 0 }).then((res) => {
    if (res.code == 200) {
      unitNameobj.value = res.data;
    }
  });
}
getList2();
/** 查询基础数据-车站数据列表 */
function getList5() {
  selectManBasicStationListapi({ hasDelete: 0 }).then((res) => {
    if (res.code == 200) {
      stationNameobj.value = res.data;
    }
  });
}
getList5();
/** 查询基础数据-股道数据列表 */
function getList6() {
  selectManStationFacilityTrackListapi({ hasDelete: 0 }).then((res) => {
    if (res.code == 200) {
      trackListobj.value = res.data;
    }
  });
}
getList6();
// 取消按钮
function cancel() {
  open.value = false;
}
/** 搜索按钮操作 */
function handleQuery() {
  getList();
}
function clear() {
  forma.value = {
    stationId: "",
    stationName: "",
    trackId: "",
    trackCode: "",
    railwayName: "",
    unitName: "",
    unitId: "",
    railwayId: "",
    startMileage: "",
    endMileage: "",
    startMileageNumber: "",
    endMileageNumber: "",
    railCode: "",
    layDate: "",
    healthIndex: "",
    hiddenDangerLevel: "",
    equipmentUsageFrequency: "",
    oee: "",
    mtbf: "",
    mttr: "",
    zone: "",
    workArea: "",
    user: "",
    hasDelete: "",
  };
}
/** 新增按钮操作 */
function handleAdd() {
  open.value = true;
  clear();
  title.value = "移观测桩信息新增";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  title.value = "移观测桩信息修改";
  editobj.value = JSON.parse(JSON.stringify(row));
  forma.value = JSON.parse(JSON.stringify(row));
  open.value = true;
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["form"].validate((valid) => {
    if (valid) {
      if (forma.value.id != null) {
        const newforma = JSON.parse(JSON.stringify(forma.value));
        for (const key in editobj.value) {
          for (const keya in newforma) {
            if (key == keya && editobj.value[key] == newforma[keya] && key != "id") {
              Reflect.deleteProperty(newforma, keya);
            }
          }
        }

        if (newforma.startMileageNumber != undefined && newforma.startMileageNumber != null) {
          newforma.startMileageNumber = Math.round(newforma.startMileageNumber * 1000000) / 1000000;
        }
        if (newforma.endMileageNumber != undefined && newforma.endMileageNumber != null) {
          newforma.endMileageNumber = Math.round(newforma.endMileageNumber * 1000000) / 1000000;
        }
        if (Object.keys(newforma).length <= 1) {
          open.value = false;
        } else {
          editManRailFacilityObservationPileapi(newforma).then((res) => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        }
      } else {
        forma.value.startMileageNumber = Math.round(forma.value.startMileageNumber * 1000000) / 1000000;
        forma.value.endMileageNumber = Math.round(forma.value.endMileageNumber * 1000000) / 1000000;
        insertManRailFacilityObservationPileapi(forma.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal
    .confirm("是否确认删除")
    .then(function () {
      return deleteManRailFacilityObservationPileByIdapi({ id: row.id });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
</script>
<style scoped lang="scss">
.el-form--inline .el-form-item.formitemcla {
  margin-right: 5px !important;
}
</style>
